package com.zsck.openali.service;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;


import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.LogUtils;
import com.zsck.openali.util.Const;

import java.util.List;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

public class AlipayForestMonitor {


    /**
     * 启动支付宝界面
     * adb shell am start com.eg.android.AlipayGphone/com.eg.android.AlipayGphone.AlipayLogin
     */
    public static void startAlipay(Context mContext) {
        Intent intent = new Intent();
        intent.setPackage("com.eg.android.AlipayGphone");
        intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
        intent.setClassName("com.eg.android.AlipayGphone", "com.eg.android.AlipayGphone.AlipayLogin");
        mContext.startActivity(intent);
    }

    /**
     * 自动点击打开转账界面
     */
    public static void enterTransfer(AccessibilityNodeInfo nodeInfo) {
        if (nodeInfo != null) {
            // 找到界面中蚂蚁森林的文字
            List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("转账");

            if (list == null) {
                Log.d(Const.TAG, "enterForestUI finding no");
                return;
            }

            for (AccessibilityNodeInfo item : list) {
                /**
                 *  蚂蚁森林本身不可点击，但是他的父控件可以点击
                 */
                AccessibilityNodeInfo parent = item.getParent();
                if (null != parent && parent.isClickable()) {
                    parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    break;
                }
            }
            enterBank(nodeInfo);
        }
    }

    /**
     * 自动点击打开转账界面
     */
    public static void enterBank(AccessibilityNodeInfo nodeInfo) {
        if (nodeInfo != null) {
            // 找到界面中蚂蚁森林的文字
            List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("转到银行卡");

            if (list == null) {
                Log.d(Const.TAG, "enterForestUI finding no");
                return;
            }

            for (AccessibilityNodeInfo item : list) {
                /**
                 *  蚂蚁森林本身不可点击，但是他的父控件可以点击
                 */
                AccessibilityNodeInfo parent = item.getParent();
                if (null != parent && parent.isClickable()) {
                    parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    break;
                }
            }
            //打开银行卡转账
            enterInBankTrans(nodeInfo);
        }
    }

    public static void enterInBankTrans(AccessibilityNodeInfo nodeInfo) {
        Log.d(Const.TAG, "enterForestUI ");

        if (nodeInfo != null) {
            // 找到界面中蚂蚁森林的文字
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {

                List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = nodeInfo.findAccessibilityNodeInfosByViewId("com.alipay.mobile.ui:id/content");

                for (AccessibilityNodeInfo info : accessibilityNodeInfosByViewId) {
                    LogUtils.e("info :" + info.getText());
                    if (info.getText() != null && info.getText().toString().equals("收款人姓名")) {
                        setBankName(info, "甘泉");
                    } else if (info.getText() != null && info.getText().toString().equals("收款人储蓄卡号")) {
                        setBankNum(info, "6214837835122528");
                    } else if (info.getText() == null) {
                        setAmount(info, "0.1");
                    }


                }

                choseBank(nodeInfo,"中国银行");


            }

        }
    }

    private static void showAllText(AccessibilityNodeInfo accessibilityNodeInfo) {
        if(accessibilityNodeInfo.getChildCount()==0){
            Log.v("linan","找到了view的文本是:"+accessibilityNodeInfo.getText());
            Log.v("linan","当前应用的包名是:"+accessibilityNodeInfo.getPackageName()+accessibilityNodeInfo.getClassName());
        }else{
            for(int i=0;i<accessibilityNodeInfo.getChildCount();i++){
                showAllText(accessibilityNodeInfo.getChild(i));
            }
        }
    }

   public static boolean isCanJump=false;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private static void choseBank(AccessibilityNodeInfo nodeInfo,String BankName) {
        List<AccessibilityNodeInfo> choseBank = nodeInfo.findAccessibilityNodeInfosByViewId("com.alipay.mobile.ui:id/table_right_text");
        for (AccessibilityNodeInfo info : choseBank) {
            AccessibilityNodeInfo parent = info.getParent();
            if (null != parent && parent.isClickable()&&!isCanJump) {
                isCanJump=!isCanJump;
                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            }
        }

        //搜索银行
        List<AccessibilityNodeInfo> searhBank = nodeInfo.findAccessibilityNodeInfosByViewId("com.alipay.mobile.ui:id/search_bar_inputbox");
        for (AccessibilityNodeInfo info : searhBank){
            searrchBankName(info,"招商银行");
        }
        List<AccessibilityNodeInfo> resultBank = nodeInfo.findAccessibilityNodeInfosByViewId("com.alipay.mobile.transferapp:id/hotBankNameText");
        for (AccessibilityNodeInfo info : resultBank){
            AccessibilityNodeInfo parent = info.getParent();
            if (null != parent && parent.isClickable()) {
                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            }

        }

        //下一步
        List<AccessibilityNodeInfo> payNextButton = nodeInfo.findAccessibilityNodeInfosByViewId("com.alipay.mobile.transferapp:id/btn_next");
        LogUtils.e("下一步"+payNextButton.size());
        for (AccessibilityNodeInfo info : payNextButton) {
            info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }

        //确定转账
        List<AccessibilityNodeInfo> payButton = nodeInfo.findAccessibilityNodeInfosByViewId("com.alipay.mobile.transferapp:id/payOk");
        LogUtils.e("确定转账"+payButton.size());
        for (AccessibilityNodeInfo info : payButton) {
            info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
           /* AccessibilityNodeInfo parent = info.getParent();
            if (null != parent && parent.isClickable()) {
                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            }*/
        }
        //继续转账
        List<AccessibilityNodeInfo> payContniuButton = nodeInfo.findAccessibilityNodeInfosByViewId("com.alipay.android.phone.wealth.common:id/confirmBtn");
        LogUtils.e("继续转账"+payButton.size());
        for (AccessibilityNodeInfo info : payContniuButton) {

            AccessibilityNodeInfo parent = info.getParent();
            if (null != parent && parent.isClickable()) {
                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            }
        }

        //立即付款
       List<AccessibilityNodeInfo> payNowText = nodeInfo.findAccessibilityNodeInfosByText("立即付款");
        for (AccessibilityNodeInfo info : payNowText) {
            AccessibilityNodeInfo parent = info.getParent();
            if (null != parent && parent.isClickable()) {
                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            }
        }

        List<AccessibilityNodeInfo> payText = nodeInfo.findAccessibilityNodeInfosByText("使用密碼");
        for (AccessibilityNodeInfo info : payText) {
            AccessibilityNodeInfo parent = info.getParent();
            if (null != parent && parent.isClickable()) {
                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            }
        }
        showAllText(nodeInfo);
        /*List<AccessibilityNodeInfo> payText = nodeInfo.findAccessibilityNodeInfosByText("使用密碼");
        for (AccessibilityNodeInfo info : payText) {
            AccessibilityNodeInfo parent = info.getParent();
            if (null != parent && parent.isClickable()) {
                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                break;
            }
        }*/
    }

    public static void searrchBankName(AccessibilityNodeInfo info, String userName) {
        Bundle arguments = new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo
                .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, userName);
        info.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
    }

    public static void setBankName(AccessibilityNodeInfo info, String userName) {
        Bundle arguments = new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo
                .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, userName);
        info.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
    }

    public static void setBankNum(AccessibilityNodeInfo info, String bankNum) {
        Bundle arguments = new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo
                .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, bankNum);
        info.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
    }

    public static void setAmount(AccessibilityNodeInfo info, String amount) {
        Bundle arguments = new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo
                .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, amount);
        info.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static void policy(AccessibilityNodeInfo nodeInfo, String packageName, String className) {
        if (packageName.equals("com.eg.android.AlipayGphone") &&
                ("com.alipay.mobile.nebulacore.ui.H5Activity".equals(className)
                        || "com.uc.webkit.bf".equals(className))) {

            if (nodeInfo != null) {
                for (int i = 0; i < nodeInfo.getChildCount(); i++) {
                    AccessibilityNodeInfo child = nodeInfo.getChild(i);
                    if ("com.uc.webview.export.WebView".equals(child.getClassName())) {
                        Log.d(Const.TAG, "找到蚂蚁森林的 webView count = " + child.getChildCount());

                        findEveryViewNode(child);
                        break;
                    }
                }
            } else {
                Log.d(Const.TAG, "alipayPolicy = null");
            }
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static void findEveryViewNode(AccessibilityNodeInfo node) {
        if (null != node && node.getChildCount() > 0) {
            for (int i = 0; i < node.getChildCount(); i++) {
                AccessibilityNodeInfo child = node.getChild(i);
                // 有时 child 为空
                if (child == null) {
                    continue;
                }

                //Log.d(TAG, "findEveryViewNode = " + child.toString());

                String className = child.getClassName().toString();
                if ("android.widget.Button".equals(className)) {
                    Log.d(Const.TAG, "Button 的节点数据 text = " + child.getText() + ", descript = " + child.getContentDescription() + ", className = " + child.getClassName() + ", resId = " + child.getViewIdResourceName());

                    boolean isClickable = child.isClickable();
                    boolean isResIdNull = child.getViewIdResourceName() == null ? true : false;

                    /**
                     * 好友的能量不能收取，因为支付宝在onTouch事件中return true,导致不会触发OnClick方法
                     *
                     * 但是支付宝中的蚂蚁森林可以收取自己的能量
                     */
                    if (isClickable && isResIdNull) {
                        child.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        Log.d(Const.TAG, "能量球 成功点击");
                    }
                }

                // 递归调用
                findEveryViewNode(child);
            }
        }
    }

}
